import COS from 'cos-js-sdk-v5';

export const loadImg = async(data: any,imgurl:any) =>{
    var cos = new COS({
        SecretId: "AKIDoU0Qlw9C0fCbpqgXL1aBtXBlALI4cbOR",
        SecretKey: "hCi27xCH9rQmy1rbFl4EIF3lU4Wq374s",
      });

      var tengxun_cos = {
        Bucket: "eryuemo-1304211956",
        Region: "ap-guangzhou",
      };
      const key =
            new Date().valueOf() + Math.random().toString(36).slice(-8) + ".jpeg";  //所以我这里生成的key是随机字符串
          
            await new Promise<void>((resolve, reject) => {
            cos.putObject(
            {
              Bucket: tengxun_cos.Bucket /* 必须 */,
              Region: tengxun_cos.Region /* 存储桶所在地域，必须字段 */,
              Key: key /* 必须切必须唯一 */,
              StorageClass: "STANDARD",
              Body: data, // 上传文件对象
              onProgress: (progressData: any) => {
                // progressData: 上传过程中的实时进度对象
                // console.log(JSON.stringify(progressData));
              },
            },
            (err: any, data: any) => {
              if (err == null && data.statusCode == 200) {
                imgurl[0] = "https://" + data.Location;
                resolve();
              } else {
                reject(err);
              }
            }
          );
            })
}